package com.youkeyi.ddy.cloud.application.applet.crm.customer.controller;

import com.youkeyi.ddy.cloud.application.applet.support.service.UserSupportService;
import com.youkeyi.ddy.cloud.common.api.application.applet.crm.customer.AppletCustomerCloudService;
import com.youkeyi.ddy.cloud.common.api.application.applet.crm.customer.dto.AppletCustomerDetailCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.applet.crm.customer.dto.AppletCustomerListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.applet.crm.customer.dto.AppletCustomerListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.applet.crm.customer.dto.AppletCustomerTagListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.applet.crm.customer.param.AppletCustomerCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.applet.crm.customer.param.AppletCustomerListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.applet.crm.customer.param.AppletCustomerUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.system.todo.param.SystemTodoCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.system.user.dto.SystemUserListByOrgCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.user.dto.SystemUserListByOrgItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.user.param.SystemUserListByOrgCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.customer.CrmCustomerCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.customer.dto.CrmCustomerListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.customer.dto.CrmCustomerListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.customer.param.CrmCustomerCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.customer.param.CrmCustomerQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.customer.param.CrmCustomerUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.tag.PublicTagCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.tag.dto.PublicTagQueryBindListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.tag.dto.PublicTagQueryBindListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.tag.param.PublicTagQueryBindCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.todo.PublicTodoCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.todo.param.PublicTodoCreateCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.CheckUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * AppletCustomerController
 *
 * @Author liuxiawang
 * @Date 2024/3/1 3:43 PM
 * @Location shenzhen.china
 */
@Slf4j
@Service
public class AppletCustomerController implements AppletCustomerCloudService {
    
    @Autowired
    private CrmCustomerCloudService crmCustomerCloudService;
    @Autowired
    private PublicTagCloudService publicTagCloudService;
    @Autowired
    private PublicTodoCloudService publicTodoCloudService;
    @Autowired
    private UserSupportService userSupportService;

    @Override
    public AppletCustomerListCloudDto list(AppletCustomerListCloudParam param) {
        AppletCustomerListCloudDto dto = new AppletCustomerListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        String positionCode = YoukeyiSystemContext.getPositionCode();
        String organizationKey = YoukeyiSystemContext.getOrganizationKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }
        CrmCustomerQueryCloudParam customerQueryCloudParam = new CrmCustomerQueryCloudParam();

        // 判断当前登陆人是主管、副主管还是普通成员
        if (StringUtils.isBlank(positionCode)) {
            dto.setErrorMessage(ResponseCodeEnum.SYSTEM_UNKNOWN_POSITION_CODE_ERROR.getMessage());
            return dto;
        }
        // 主管副主管
        if (positionCode.endsWith("-ZG") || positionCode.endsWith("-FZG")) {
            // 查询他的组织下属人员的
            List<String> userKeys = new ArrayList<>();
            if (StringUtils.isNotBlank(organizationKey)) {
                SystemUserListByOrgCloudParam userListByOrgCloudParam = new SystemUserListByOrgCloudParam();
                userListByOrgCloudParam.setOrganizationPrimaryKeys(Collections.singletonList(organizationKey));
                SystemUserListByOrgCloudDto userDtos = userSupportService.listUserByOrg(userListByOrgCloudParam);
                if (null != userDtos && CollectionUtils.isNotEmpty(userDtos.getRows())) {
                    userKeys.addAll(userDtos.getRows().stream().map(SystemUserListByOrgItemCloudDto::getUserPrimaryKey).collect(Collectors.toList()));
                }
            }
            if (CollectionUtils.isEmpty(userKeys)) {
                dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
                return dto;
            }
            // 查询出来的下属用户key
            customerQueryCloudParam.setPublicUserKeys(userKeys);
        }
        // 普通成员
        else if (positionCode.endsWith("-CY")) {
            customerQueryCloudParam.setPublicUserKeys(Collections.singletonList(YoukeyiSystemContext.getUserPrimaryKey()));
        }
        // 超管
        else if (positionCode.equals("superadmin")) {
            // 超级管理员不需要增加什么过滤
        }
        // 管理员
        else if (positionCode.equals("-admin")) {
            // 管理员看主体的
            customerQueryCloudParam.setPublicCorpKeys(Collections.singletonList(corpKey));
        }
        // 未知的岗位
        else {
            dto.setErrorMessage(ResponseCodeEnum.SYSTEM_UNKNOWN_POSITION_CODE_ERROR.getMessage());
            return dto;
        }


        customerQueryCloudParam.setCustomerPrimaryKeys(param.getCustomerPrimaryKeys());
        customerQueryCloudParam.setCodes(param.getCodes());
        customerQueryCloudParam.setStatuses(param.getStatuses());
        customerQueryCloudParam.setCustomerTypeKeys(param.getCustomerTypeKeys());
        customerQueryCloudParam.setNameFuzzy(param.getNameFuzzy());
        customerQueryCloudParam.setEnglishNameFuzzy(param.getEnglishNameFuzzy());
        customerQueryCloudParam.setPhoneFuzzy(param.getPhoneFuzzy());
        customerQueryCloudParam.setIdCardFuzzy(param.getIdCardFuzzy());
        customerQueryCloudParam.setEmailFuzzy(param.getEmailFuzzy());
        customerQueryCloudParam.setCompanyFuzzy(param.getCompanyFuzzy());
        customerQueryCloudParam.setLicenseCodeFuzzy(param.getLicenseCodeFuzzy());
        customerQueryCloudParam.setCreateTimeBegin(param.getCreateTimeBegin());
        customerQueryCloudParam.setCreateTimeEnd(param.getCreateTimeEnd());
        customerQueryCloudParam.setKeywords(param.getKeyword());
        customerQueryCloudParam.setPageSize(param.getPageSize());
        customerQueryCloudParam.setCurrentPage(param.getCurrentPage());
        CrmCustomerListCloudDto customerDtos = crmCustomerCloudService.query(customerQueryCloudParam);
        if (null != customerDtos && CollectionUtils.isNotEmpty(customerDtos.getRows())) {
            PublicTagQueryBindCloudParam tagQueryBindCloudParam = new PublicTagQueryBindCloudParam();
            tagQueryBindCloudParam.setTargetPrimaryKeys(customerDtos.getRows().stream().map(CrmCustomerListItemCloudDto::getCustomerPrimaryKey).collect(Collectors.toList()));
            PublicTagQueryBindListCloudDto tagDtos = publicTagCloudService.queryBind(tagQueryBindCloudParam);
            Map<String, List<PublicTagQueryBindListItemCloudDto>> tagMaps = new HashMap<>();
            if (null != tagDtos && CollectionUtils.isNotEmpty(tagDtos.getRows())) {
                tagMaps.putAll(tagDtos.getRows().stream().collect(Collectors.groupingBy(PublicTagQueryBindListItemCloudDto::getTargetPrimaryKey)));
            }

            List<AppletCustomerListItemCloudDto> items = new ArrayList<>();
            customerDtos.getRows().forEach(customer -> {
                AppletCustomerListItemCloudDto item = new AppletCustomerListItemCloudDto();
                item.setCustomerPrimaryKey(customer.getCustomerPrimaryKey());
                item.setCsCorpKey(customer.getCsCorpKey());
                item.setCsUserKey(customer.getCsUserKey());
                item.setCsUserName(customer.getCsUserName());
                item.setSourceCorpKey(customer.getSourceCorpKey());
                item.setSourceUserKey(customer.getSourceUserKey());
                item.setSourceUserName(customer.getSourceUserName());
                item.setSourceChannel(customer.getSourceChannel());
                item.setSourceChannelName(customer.getSourceChannelName());
                item.setSourceChannelPersonName(customer.getSourceChannelPersonName());
                item.setAdviserCorpKey(customer.getAdviserCorpKey());
                item.setAdviserUserKey(customer.getAdviserUserKey());
                item.setAdviserUserName(customer.getAdviserUserName());
                item.setServerCorpKey(customer.getServerCorpKey());
                item.setServerUserKey(customer.getServerUserKey());
                item.setServerUserName(customer.getServerUserName());
                item.setCustomerTypeKey(customer.getCustomerTypeKey());
                item.setCustomerTypeKeyName(customer.getCustomerTypeKeyName());
                item.setCustomerCode(customer.getCode());
                item.setEnglishName(customer.getEnglishName());
                item.setName(customer.getName());
                item.setWechatId(customer.getWechatId());
                item.setSex(customer.getSex());
                item.setHeight(customer.getHeight());
                item.setWeight(customer.getWeight());
                item.setBirthday(customer.getBirthday());
                item.setAge(customer.getAge());
                item.setPhone(customer.getPhone());
                item.setEmail(customer.getEmail());
                item.setStatus(customer.getStatus());
                item.setStatusName(customer.getStatusName());
                item.setCompany(customer.getCompany());
                item.setJobTitle(customer.getJobTitle());
                item.setIndustry(customer.getIndustry());
                item.setLicenseName(customer.getLicenseName());
                item.setIdCard(customer.getIdCard());
                item.setAddress(customer.getAddress());
                item.setEducational(customer.getEducational());
                item.setEmergencyContactName(customer.getEmergencyContactName());
                item.setEmergencyContactPhone(customer.getEmergencyContactPhone());
                item.setEmergencyContactRelationship(customer.getEmergencyContactRelationship());
                item.setAccountType(customer.getAccountType());
                item.setAccountName(customer.getAccountName());
                item.setBank(customer.getBank());
                item.setBankNo(customer.getBankNo());
                item.setLicenseCode(customer.getLicenseCode());
                item.setIncomeRmb(customer.getIncomeRmb());
                item.setIncomeHkd(customer.getIncomeHkd());
                item.setLeadSource(customer.getLeadSource());
                item.setPotentialDemandKey(customer.getPotentialDemandKey());
                item.setPotentialDemandName(customer.getPotentialDemandName());
                item.setProvince(customer.getProvince());
                item.setCity(customer.getCity());
                item.setAdviserFlag(customer.getAdviserFlag());
                item.setAdviserFlagUserKey(customer.getAdviserFlagUserKey());
                item.setAdviserFlagUserName(customer.getAdviserFlagUserName());
                item.setCsFlag(customer.getCsFlag());
                item.setCsFlagUserKey(customer.getCsFlagUserKey());
                item.setCsFlagUserName(customer.getCsFlagUserName());
                item.setPlace(customer.getPlace());
                item.setInitialFollowTime(customer.getInitialFollowTime());
                item.setInitialFollowDesc(customer.getInitialFollowDesc());
                item.setRemark(customer.getRemark());
                item.setEvaluationDate(customer.getEvaluationDate());
                item.setVisaExpirationDate(customer.getVisaExpirationDate());
                item.setAnnexKey(customer.getAnnexKey());
                item.setIsDeleted(customer.getIsDeleted());
                item.setIsDeletedName(customer.getIsDeletedName());
                item.setCreateUserKey(customer.getCreateUserKey());
                item.setCreateUserName(customer.getCreateUserName());
                item.setCreateTime(customer.getCreateTime());
                if (tagMaps.containsKey(customer.getCustomerPrimaryKey())) {
                    tagMaps.get(customer.getCustomerPrimaryKey()).forEach(tag -> {
                        AppletCustomerTagListItemCloudDto tagItem = new AppletCustomerTagListItemCloudDto();
                        tagItem.setTagPrimaryKey(tag.getTagPrimaryKey());
                        tagItem.setTagName(tag.getName());
                        tagItem.setIcon(tag.getIcon());
                        tagItem.setIconColor(tag.getIconColor());
                        item.getTags().add(tagItem);
                    });
                }
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(customerDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public AppletCustomerDetailCloudDto detail(String customerPrimaryKey) {
        AppletCustomerDetailCloudDto dto = new AppletCustomerDetailCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (StringUtils.isBlank(customerPrimaryKey)) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        AppletCustomerListCloudParam customerListCloudParam = new AppletCustomerListCloudParam();
        customerListCloudParam.setCustomerPrimaryKeys(Collections.singletonList(customerPrimaryKey));
        AppletCustomerListCloudDto customers = list(customerListCloudParam);
        if (null != customers && CollectionUtils.isNotEmpty(customers.getRows())) {
            AppletCustomerListItemCloudDto customer = customers.getRows().get(0);

            dto.setCustomerPrimaryKey(customer.getCustomerPrimaryKey());
            dto.setCsCorpKey(customer.getCsCorpKey());
            dto.setCsUserKey(customer.getCsUserKey());
            dto.setCsUserName(customer.getCsUserName());
            dto.setSourceCorpKey(customer.getSourceCorpKey());
            dto.setSourceUserKey(customer.getSourceUserKey());
            dto.setSourceUserName(customer.getSourceUserName());
            dto.setSourceChannel(customer.getSourceChannel());
            dto.setAdviserCorpKey(customer.getAdviserCorpKey());
            dto.setAdviserUserKey(customer.getAdviserUserKey());
            dto.setAdviserUserName(customer.getAdviserUserName());
            dto.setServerCorpKey(customer.getServerCorpKey());
            dto.setServerUserKey(customer.getServerUserKey());
            dto.setServerUserName(customer.getServerUserName());
            dto.setCustomerTypeKey(customer.getCustomerTypeKey());
            dto.setCustomerTypeKeyName(customer.getCustomerTypeKeyName());
            dto.setCustomerCode(customer.getCustomerCode());
            dto.setEnglishName(customer.getEnglishName());
            dto.setName(customer.getName());
            dto.setWechatId(customer.getWechatId());
            dto.setSex(customer.getSex());
            dto.setBirthday(customer.getBirthday());
            dto.setPhone(customer.getPhone());
            dto.setIdCard(customer.getIdCard());
            dto.setEmail(customer.getEmail());
            dto.setAddress(customer.getAddress());
            dto.setEducational(customer.getEducational());
            dto.setStatus(customer.getStatus());
            dto.setStatusName(customer.getName());
            dto.setCompany(customer.getCompany());
            dto.setJobTitle(customer.getJobTitle());
            dto.setIndustry(customer.getIndustry());
            dto.setBank(customer.getBank());
            dto.setBankNo(customer.getBankNo());
            dto.setLicenseName(customer.getLicenseName());
            dto.setLicenseCode(customer.getLicenseCode());
            dto.setIncomeRmb(customer.getIncomeRmb());
            dto.setIncomeHkd(customer.getIncomeHkd());
            dto.setLeadSource(customer.getLeadSource());
            dto.setPotentialDemandKey(customer.getPotentialDemandKey());
            dto.setPotentialDemandName(customer.getPotentialDemandName());
            dto.setProvince(customer.getProvince());
            dto.setCity(customer.getCity());
            dto.setAdviserFlag(customer.getAdviserFlag());
            dto.setAdviserFlagUserKey(customer.getAdviserFlagUserKey());
            dto.setAdviserFlagUserName(customer.getAdviserFlagUserName());
            dto.setCsFlag(customer.getCsFlag());
            dto.setCsFlagUserKey(customer.getCsFlagUserKey());
            dto.setCsFlagUserName(customer.getCsFlagUserName());
            dto.setPlace(customer.getPlace());
            dto.setInitialFollowTime(customer.getInitialFollowTime());
            dto.setInitialFollowDesc(customer.getInitialFollowDesc());
            dto.setRemark(customer.getRemark());
            dto.setEvaluationDate(customer.getEvaluationDate());
            dto.setVisaExpirationDate(customer.getVisaExpirationDate());
            dto.setAnnexKey(customer.getAnnexKey());
            dto.setIsDeleted(customer.getIsDeleted());
            dto.setIsDeletedName(customer.getIsDeletedName());
            dto.setCreateUserKey(customer.getCreateUserKey());
            dto.setCreateUserName(customer.getCreateUserName());
            dto.setCreateTime(customer.getCreateTime());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(AppletCustomerCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getName())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        if (StringUtils.isNotBlank(param.getEmail())) {
            if (!CheckUtils.isValidEmain(param.getEmail())) {
                rc.setErrorMessage(ResponseCodeEnum.CRM_EMAIL_INVALID.getMessage());
                return rc;
            }
        }

        CrmCustomerCreateCloudParam customerCreateCloudParam = new CrmCustomerCreateCloudParam();
        customerCreateCloudParam.setCorpKey(corpKey);
        customerCreateCloudParam.setCsCorpKey(StringUtils.defaultIfEmpty(param.getCsCorpKey(), corpKey));
        customerCreateCloudParam.setCsUserKey(StringUtils.defaultIfEmpty(param.getCsUserKey(), YoukeyiSystemContext.getUserPrimaryKey()));
        customerCreateCloudParam.setCsUserName(StringUtils.defaultIfEmpty(param.getCsUserName(), YoukeyiSystemContext.getUserName()));
        customerCreateCloudParam.setSourceCorpKey(param.getSourceCorpKey());
        customerCreateCloudParam.setSourceUserKey(param.getSourceUserKey());
        customerCreateCloudParam.setSourceUserName(param.getSourceUserName());
        customerCreateCloudParam.setSourceChannel(param.getSourceChannel());
        customerCreateCloudParam.setAdviserCorpKey(param.getAdviserCorpKey());
        customerCreateCloudParam.setAdviserUserKey(param.getAdviserUserKey());
        customerCreateCloudParam.setAdviserUserName(param.getAdviserUserName());
        customerCreateCloudParam.setServerCorpKey(param.getServerCorpKey());
        customerCreateCloudParam.setServerUserKey(param.getServerUserKey());
        customerCreateCloudParam.setServerUserName(param.getServerUserName());
        customerCreateCloudParam.setCustomerTypeKey(param.getCustomerTypeKey());
        customerCreateCloudParam.setCode(param.getCode());
        customerCreateCloudParam.setEnglishName(param.getEnglishName());
        customerCreateCloudParam.setName(param.getName());
        customerCreateCloudParam.setWechatId(param.getPhone());
        customerCreateCloudParam.setSex(param.getSex());
        customerCreateCloudParam.setHeight(param.getHeight());
        customerCreateCloudParam.setWeight(param.getWeight());
        customerCreateCloudParam.setBirthday(param.getBirthday());
        customerCreateCloudParam.setPhone(param.getPhone());
        customerCreateCloudParam.setIdCard(param.getIdCard());
        customerCreateCloudParam.setEmail(param.getEmail());
        customerCreateCloudParam.setAddress(param.getAddress());
        customerCreateCloudParam.setEducational(param.getEducational());
        customerCreateCloudParam.setEmergencyContactName(param.getEmergencyContactName());
        customerCreateCloudParam.setEmergencyContactPhone(param.getEmergencyContactPhone());
        customerCreateCloudParam.setEmergencyContactRelationship(param.getEmergencyContactRelationship());
        customerCreateCloudParam.setStatus(param.getStatus());
        customerCreateCloudParam.setCompany(param.getCompany());
        customerCreateCloudParam.setJobTitle(param.getJobTitle());
        customerCreateCloudParam.setIndustry(param.getIndustry());
        customerCreateCloudParam.setAccountType(param.getAccountType());
        customerCreateCloudParam.setAccountName(param.getAccountName());
        customerCreateCloudParam.setBank(param.getBank());
        customerCreateCloudParam.setBankNo(param.getBankNo());
        customerCreateCloudParam.setLicenseName(param.getLicenseName());
        customerCreateCloudParam.setLicenseCode(param.getLicenseCode());
        customerCreateCloudParam.setIncomeRmb(param.getIncomeRmb());
        customerCreateCloudParam.setIncomeHkd(param.getIncomeHkd());
        customerCreateCloudParam.setLeadSource(param.getLeadSource());
        customerCreateCloudParam.setPotentialDemandKey(param.getPotentialDemandKey());
        customerCreateCloudParam.setPotentialDemandName(param.getPotentialDemandName());
        customerCreateCloudParam.setProvince(param.getProvince());
        customerCreateCloudParam.setCity(param.getCity());
        customerCreateCloudParam.setAdviserFlag(param.getAdviserFlag());
        customerCreateCloudParam.setAdviserFlagUserKey(param.getAdviserFlagUserKey());
        customerCreateCloudParam.setAdviserFlagUserName(param.getAdviserFlagUserName());
        customerCreateCloudParam.setCsFlag(param.getCsFlag());
        customerCreateCloudParam.setCsFlagUserKey(param.getCsFlagUserKey());
        customerCreateCloudParam.setCsFlagUserName(param.getCsFlagUserName());
        customerCreateCloudParam.setPlace(param.getPlace());
        customerCreateCloudParam.setInitialFollowTime(param.getInitialFollowTime());
        customerCreateCloudParam.setInitialFollowDesc(param.getInitialFollowDesc());
        customerCreateCloudParam.setRemark(param.getRemark());
        customerCreateCloudParam.setEvaluationDate(param.getEvaluationDate());
        customerCreateCloudParam.setVisaExpirationDate(param.getVisaExpirationDate());
        customerCreateCloudParam.setAnnexKey(param.getAnnexKey());
        customerCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        customerCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        ResponseContent responseContent = crmCustomerCloudService.create(customerCreateCloudParam);
        if (null != responseContent && responseContent.isSuccess()) {
            this.createTodo(param.getTodoParam(), responseContent.getKey());
        }
        return responseContent;
    }

    @Override
    public ResponseContent update(AppletCustomerUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getCustomerPrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmCustomerUpdateCloudParam customerUpdateCloudParam = new CrmCustomerUpdateCloudParam();
        customerUpdateCloudParam.setCustomerPrimaryKey(param.getCustomerPrimaryKey());
        customerUpdateCloudParam.setSourceCorpKey(param.getSourceCorpKey());
        customerUpdateCloudParam.setSourceUserKey(param.getSourceUserKey());
        customerUpdateCloudParam.setSourceUserName(param.getSourceUserName());
        customerUpdateCloudParam.setSourceChannel(param.getSourceChannel());
        customerUpdateCloudParam.setAdviserCorpKey(param.getAdviserCorpKey());
        customerUpdateCloudParam.setAdviserUserKey(param.getAdviserUserKey());
        customerUpdateCloudParam.setAdviserUserName(param.getAdviserUserName());
        customerUpdateCloudParam.setCustomerTypeKey(param.getCustomerTypeKey());
        customerUpdateCloudParam.setCode(param.getCode());
        customerUpdateCloudParam.setEnglishName(param.getEnglishName());
        customerUpdateCloudParam.setName(param.getName());
        customerUpdateCloudParam.setWechatId(param.getWechatId());
        customerUpdateCloudParam.setSex(param.getSex());
        customerUpdateCloudParam.setHeight(param.getHeight());
        customerUpdateCloudParam.setWeight(param.getWeight());
        customerUpdateCloudParam.setBirthday(param.getBirthday());
        customerUpdateCloudParam.setPhone(param.getPhone());
        customerUpdateCloudParam.setIdCard(param.getIdCard());
        customerUpdateCloudParam.setEmail(param.getEmail());
        customerUpdateCloudParam.setAddress(param.getAddress());
        customerUpdateCloudParam.setEducational(param.getEducational());
        customerUpdateCloudParam.setEmergencyContactName(param.getEmergencyContactName());
        customerUpdateCloudParam.setEmergencyContactPhone(param.getEmergencyContactPhone());
        customerUpdateCloudParam.setEmergencyContactRelationship(param.getEmergencyContactRelationship());
        customerUpdateCloudParam.setStatus(param.getStatus());
        customerUpdateCloudParam.setCompany(param.getCompany());
        customerUpdateCloudParam.setJobTitle(param.getJobTitle());
        customerUpdateCloudParam.setIndustry(param.getIndustry());
        customerUpdateCloudParam.setAccountType(param.getAccountType());
        customerUpdateCloudParam.setAccountName(param.getAccountName());
        customerUpdateCloudParam.setBank(param.getBank());
        customerUpdateCloudParam.setBankNo(param.getBankNo());
        customerUpdateCloudParam.setLicenseName(param.getLicenseName());
        customerUpdateCloudParam.setLicenseCode(param.getLicenseCode());
        customerUpdateCloudParam.setIncomeRmb(param.getIncomeRmb());
        customerUpdateCloudParam.setIncomeHkd(param.getIncomeHkd());
        customerUpdateCloudParam.setLeadSource(param.getLeadSource());
        customerUpdateCloudParam.setPotentialDemandKey(param.getPotentialDemandKey());
        customerUpdateCloudParam.setPotentialDemandName(param.getPotentialDemandName());
        customerUpdateCloudParam.setProvince(param.getProvince());
        customerUpdateCloudParam.setCity(param.getCity());
        customerUpdateCloudParam.setAdviserFlag(param.getAdviserFlag());
        customerUpdateCloudParam.setAdviserFlagUserKey(param.getAdviserFlagUserKey());
        customerUpdateCloudParam.setAdviserFlagUserName(param.getAdviserFlagUserName());
        customerUpdateCloudParam.setCsFlag(param.getCsFlag());
        customerUpdateCloudParam.setCsFlagUserKey(param.getCsFlagUserKey());
        customerUpdateCloudParam.setCsFlagUserName(param.getCsFlagUserName());
        customerUpdateCloudParam.setPlace(param.getPlace());
        customerUpdateCloudParam.setInitialFollowTime(param.getInitialFollowTime());
        customerUpdateCloudParam.setInitialFollowDesc(param.getInitialFollowDesc());
        customerUpdateCloudParam.setRemark(param.getRemark());
        customerUpdateCloudParam.setEvaluationDate(param.getEvaluationDate());
        customerUpdateCloudParam.setVisaExpirationDate(param.getVisaExpirationDate());
        customerUpdateCloudParam.setAnnexKey(param.getAnnexKey());
        customerUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        customerUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        ResponseContent responseContent = crmCustomerCloudService.update(customerUpdateCloudParam);
        if (null != responseContent && responseContent.isSuccess()) {
            this.createTodo(param.getTodoParam(), param.getCustomerPrimaryKey());
        }
        return responseContent;
    }

    /**
     * 创建提醒
     */
    private void createTodo(SystemTodoCreateCloudParam param, String customerPrimaryKey) {
        try {
            // 站内信
            if (null != param && null != param.getTodoRemindType() && "in-mail".equals(param.getTodoRemindType())) {
                PublicTodoCreateCloudParam todoCreateCloudParam = new PublicTodoCreateCloudParam();
                todoCreateCloudParam.setCorpKey(YoukeyiSystemContext.getCorpPrimaryKey());
                todoCreateCloudParam.setUserKey(YoukeyiSystemContext.getUserPrimaryKey());
                todoCreateCloudParam.setCustomerKey(customerPrimaryKey);
                todoCreateCloudParam.setContent(param.getTodoContent());
                todoCreateCloudParam.setStatus(1);
                todoCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
                todoCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
                publicTodoCloudService.create(todoCreateCloudParam);
            }
        } catch (Exception e) {
            log.error("创建提醒失败", e);
        }
    }

}
